<?php
defined('JOC') or die;

class Page extends Template
{
  private $_page        = Array() ;
  private $_file        = '' ;
  private $_text        = '' ;
  private $_page_name   = 'home' ;
  private $_section     = Array() ;
  private $_module      = Array() ;
  private $_meta        = Array() ;
  private $_ext_js			= Array() ;
  private $_header_js 	= Array() ;
  private $_footer_js 	= Array() ;
  private $_meta_tags 	= Array() ;
  private $_links       = Array() ;
  private $_modules_instances = Array() ;
  private $properties		= Array() ;
  //in the future this will be processed on __construct and will be configured in config.php 
  private $on_error_page_order = Array();
  
  function __construct()
  {

    global $db ;
    $this->on_error_page_order = ArraY(R::all('page') ? R::all('page') : $this->_page_name, 'home' , '404' , 'error' ) ;
    //if the page requested doesn´t exists will try to show the secuence defined in $on_error_page_order

    $try = 0 ;

    do {
        $this->_page_name =  $this->on_error_page_order[$try] ;

//        debug::pr($try);

//        debug::pr($this->_page_name);

        //obtain the information of the page we want to show 
        $sql = "SELECT P.* , T.path AS theme , PT.title as template_title , 
                  PT.file_name as template_file 
                  FROM {prefix}page AS P 
                  INNER JOIN {prefix}page_template AS PT ON PT.page_template_id = P.page_template_id
                  INNER JOIN {prefix}theme AS T ON T.theme_id = P.theme_id
                  WHERE P.title = '{$this->_page_name}' LIMIT 1 ; " ;
        //update this line to the new db.class
        $this->_page = $db->query($sql,true,10) ;

        $this->_page = isset($this->_page[0]) ? $this->_page[0] : Array() ;

        if($this->_page){

          parent::__construct($this->_page['template_file']) ;

          /*
           * process section and this method will run $this->_processmodule();
           */
          
          $sql = " SELECT * FROM {prefix}site_property ; " ;
          
          $this->properties = $db->query($sql) ;
          
          /* post process methods */
          
          $this->_process_section() 
          
          ->_process_meta()
          
          ->_process_js()

          ->_process_link() ;

        } ;
        
        $try++ ;
        
    } while ($try < count($this->on_error_page_order) && empty($this->_page) ) ;

  }
  private function _process_section()
  {
//  all this method will do the stuff that in the past execute the class modulo
    global $db ; 
    $sql  = " SELECT PSMI.section_id , PSMI.page_section_module_instance_id ,MI.module_instance_id, MI.module_id , M.class , MT.html_name , T.path as theme_path " ;
    $sql  .= " FROM {prefix}page_section_module_instance AS PSMI " ;
    $sql  .= " INNER JOIN {prefix}module_instance AS MI ON MI.module_instance_id = PSMI.module_instance_id " ;
    $sql  .= " INNER JOIN {prefix}module AS M ON M.module_id = MI.module_id " ;
    $sql  .= " INNER JOIN {prefix}module_template AS MT ON MT.module_template_id = MI.module_template_id " ;
    $sql  .= " INNER JOIN {prefix}theme AS T ON T.theme_id = MT.theme_id " ;
    $sql  .= " WHERE PSMI.page_id = {$this->_page['page_id']} " ;
    
    $this->_modules_instances = $db->query($sql) ;

    foreach($this->_modules_instances as &$module_instance)
    {
//    	 debug::pr($module_instance,'',false);
    	
       $instance = new module_instance($module_instance);
 
       $this->addModuleToRegion($instance->get() , $module_instance['section_id'] ) ;
    }
    
//    debug::pr('end','',true);
    return $this ;
  }
  private function _process_js()
  {
  	/*
  	 * the field 'place' indicates where the script will be placed 
  	 * external-header : 1
  	 * header : 2
  	 * footer : 3
  	 * */
    global $db ;
    $sql = " SELECT * FROM {prefix}page_javascript WHERE page_id = {$this->_page['page_id']} ORDER BY `order` ASC ; " ;
    $_page_script = $db->query($sql,true,9999) ;
    
    $sql = " SELECT * FROM {prefix}site_javascript ORDER BY `order` ASC ; " ;
    $_site_script = $db->query($sql,true,9999) ;
    
    $scripts = array_merge((array)$_site_script ,(array)$_page_script ) ;
    
    if($scripts)
    {
    	foreach ($scripts as $script)
    	{
    		if($script['local'] == 1)
    		{
    			$script['src'] = seo::url_frontend().$script['src'] ;
    		}
    		switch($script['place'])
    		{
    			case 1 : $this->_ext_js[] = $script ;  
    				break ;
    			case 2 : $this->_header_js[] = $script ;
    				break ;
    		  case 3 : $this->_footer_js[] = $script ;
    				break ;
    		}
    	}
    }
    return $this ;
  }
	function get_JavaScripts()
	{
  	return $this->_ext_js ;
  }
	function get_HeaderScripts()
	{
  	return $this->_header_js ;
  }
	function get_FooterScripts()
	{
  	return $this->_footer_js ;
  }
  private function _process_link()
  {
  	global $db ;
    
    $Page_header_link = joc::model('Page_header_link') ;
    $_page_links = $Page_header_link->Select_by_page_id($this->_page['page_id'])->get() ;
    
    $sql = " SELECT * FROM {prefix}site_header_link ORDER BY `order` ASC ; " ;
    $_site_links = $db->query($sql) ;
    
    $this->_links = array_merge((array)$_site_links ,(array)$_page_links ) ;
    foreach($this->_links as &$link)
    {
      if($link['local'] == 1)
      {
    		$link['href'] = seo::url_frontend().$link['href'] ;
    	}
    }
    return $this ;
  }
	function get_Links(){
  	return $this->_links ;
  }
  private function _process_meta()
  {
    global $db ;
    $Page_meta_tag = joc::model('Page_meta_tag') ;
    
    $_page_meta_tags = $Page_meta_tag->Select_by_page_id($this->_page['page_id'])->get() ;
    
    $sql = " SELECT * FROM {prefix}site_meta_tag ORDER BY `order` ASC ; " ;
    $_site_meta_tags = $db->query($sql) ;
    
    $this->_meta_tags = array_merge((array)$_site_meta_tags ,(array)$_page_meta_tags ) ;
    foreach($this->_meta_tags as &$meta_tag)
    {
      $meta_tag['http_equiv'] = $meta_tag['http_equiv'] != '' ? "http-equiv=\"{$meta_tag['http_equiv']}\"" 	: '' ;
      $meta_tag['name'] 			= $meta_tag['name'] 			!= '' ? "name=\"{$meta_tag['name']}\"" 							: '' ;
      $meta_tag['content'] 		= $meta_tag['content'] 		!= '' ? "content=\"{$meta_tag['content']}\"" 				: '' ;
      $meta_tag['charset'] 		= $meta_tag['charset'] 		!= '' ? "charset=\"{$meta_tag['charset']}\"" 				: '' ; 
    }

    return $this ;  
  }
  function get_MetaTags(){
  	return $this->_meta_tags ;
  }
  function print_screen()
  {
    echo $this->get_string() ;
  }
}
